from django.db import models
from django.contrib.auth.models import User
from django.core.validators import MinValueValidator, MaxValueValidator
import uuid

class Movie(models.Model):
    """电影模型"""
    title = models.CharField('电影名称', max_length=200)
    director = models.CharField('导演', max_length=100)
    actors = models.TextField('主演')
    genre = models.CharField('类型', max_length=100)
    duration = models.IntegerField('时长(分钟)')
    description = models.TextField('简介')
    poster = models.ImageField('海报', upload_to='posters/', null=True, blank=True)
    rating = models.DecimalField('评分', max_digits=3, decimal_places=1, default=0.0)
    price = models.DecimalField('票价', max_digits=6, decimal_places=2)
    release_date = models.DateField('上映日期')
    is_active = models.BooleanField('是否上映', default=True)
    created_at = models.DateTimeField('创建时间', auto_now_add=True)
    updated_at = models.DateTimeField('更新时间', auto_now=True)

    class Meta:
        verbose_name = '电影'
        verbose_name_plural = '电影'

    def __str__(self):
        return self.title

class CinemaHall(models.Model):
    """影厅模型"""
    name = models.CharField('影厅名称', max_length=50)
    capacity = models.IntegerField('座位数')
    hall_type = models.CharField('影厅类型', max_length=50, choices=[
        ('2D', '2D'),
        ('3D', '3D'),
        ('IMAX', 'IMAX'),
        ('VIP', 'VIP'),
    ])
    is_active = models.BooleanField('是否可用', default=True)

    class Meta:
        verbose_name = '影厅'
        verbose_name_plural = '影厅'

    def __str__(self):
        return f"{self.name} ({self.hall_type})"

class Screening(models.Model):
    """场次模型"""
    movie = models.ForeignKey(Movie, on_delete=models.CASCADE, verbose_name='电影')
    hall = models.ForeignKey(CinemaHall, on_delete=models.CASCADE, verbose_name='影厅')
    start_time = models.DateTimeField('开始时间')
    end_time = models.DateTimeField('结束时间')
    price = models.DecimalField('票价', max_digits=6, decimal_places=2)
    is_active = models.BooleanField('是否有效', default=True)
    created_at = models.DateTimeField('创建时间', auto_now_add=True)

    class Meta:
        verbose_name = '场次'
        verbose_name_plural = '场次'
        ordering = ['start_time']

    def __str__(self):
        return f"{self.movie.title} - {self.hall.name} - {self.start_time.strftime('%Y-%m-%d %H:%M')}"

class Seat(models.Model):
    """座位模型"""
    hall = models.ForeignKey(CinemaHall, on_delete=models.CASCADE, verbose_name='影厅')
    row = models.CharField('行', max_length=10)
    column = models.IntegerField('列')
    seat_number = models.CharField('座位号', max_length=20)
    is_active = models.BooleanField('是否可用', default=True)

    class Meta:
        verbose_name = '座位'
        verbose_name_plural = '座位'
        unique_together = ['hall', 'row', 'column']

    def __str__(self):
        return f"{self.hall.name} - {self.seat_number}"

class Order(models.Model):
    """订单模型"""
    ORDER_STATUS = [
        ('pending', '待支付'),
        ('paid', '已支付'),
        ('cancelled', '已取消'),
        ('completed', '已完成'),
    ]

    order_id = models.CharField('订单号', max_length=50, unique=True, default=uuid.uuid4)
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    screening = models.ForeignKey(Screening, on_delete=models.CASCADE, verbose_name='场次')
    seats = models.ManyToManyField(Seat, verbose_name='座位')
    total_amount = models.DecimalField('总金额', max_digits=8, decimal_places=2)
    status = models.CharField('状态', max_length=20, choices=ORDER_STATUS, default='pending')
    created_at = models.DateTimeField('创建时间', auto_now_add=True)
    updated_at = models.DateTimeField('更新时间', auto_now=True)

    class Meta:
        verbose_name = '订单'
        verbose_name_plural = '订单'
        ordering = ['-created_at']

    def __str__(self):
        return f"订单 {self.order_id}"

class UserProfile(models.Model):
    """用户资料模型"""
    user = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name='用户')
    phone = models.CharField('手机号', max_length=20, blank=True)
    avatar = models.ImageField('头像', upload_to='avatars/', null=True, blank=True)
    points = models.IntegerField('积分', default=0)
    created_at = models.DateTimeField('创建时间', auto_now_add=True)

    class Meta:
        verbose_name = '用户资料'
        verbose_name_plural = '用户资料'

    def __str__(self):
        return self.user.username

class MovieReview(models.Model):
    """电影评论模型"""
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    movie = models.ForeignKey(Movie, on_delete=models.CASCADE, verbose_name='电影')
    rating = models.IntegerField('评分', validators=[MinValueValidator(1), MaxValueValidator(5)])
    comment = models.TextField('评论')
    created_at = models.DateTimeField('创建时间', auto_now_add=True)

    class Meta:
        verbose_name = '电影评论'
        verbose_name_plural = '电影评论'
        unique_together = ['user', 'movie']

    def __str__(self):
        return f"{self.user.username} 对 {self.movie.title} 的评论"
